rm(list=ls())
#=====================================================
# Coefficients Plot
#=====================================================

library(foreign)

# Load results from Daughter_KGSS_Data_Analysis.do
# set up the working directory in your computer
setwd("")

coef.t1 <- read.csv("year_LPM_KGSS_d_conscale.csv")
coef.t2 <- read.csv("year_LPM_KGSS_d_hanara.csv")


pdf("KGSS_daughter.pdf", paper="special", width=10, height=5)
layout(matrix(c(1:2), 1, 2, byrow=T))

  d1 <- subset(coef.t1, is.na(o_year)==F)
  d2 <- subset(coef.t2, is.na(o_year)==F)
  
  
  b0.all <- d1$o_coef
  se0.all <- d1$o_se
  sig0.all <- ifelse(d1$o_p<0.05,1,ifelse(d1$o_p<0.1,2,0))
    
  b1.all <- d2$o_coef
  se1.all <- d2$o_se
  sig1.all <- ifelse(d2$o_p<0.05,1,ifelse(d2$o_p<0.1,2,0))

  xid0 <- d1$o_year
  xid1 <- d2$o_year

  y0.color <- ifelse(b0.all>0,"black","black")
  y0.lty <- ifelse(sig0.all>0,1,2)
  y0.pch <- ifelse(b0.all>0,20,21)

  y1.color <- ifelse(b1.all>0,"black","black")
  y1.lty <- ifelse(sig1.all>0,1,2)
  y1.pch <- ifelse(b1.all>0,20,21)
  
plot(xid0, b0.all, xlab = "Survey Year", xlim = c(min(d1$o_year),max(d1$o_year)), main="Political Ideology (OLS)",
     ylim=c(-60,60), ylab="Coef. with 95% CI",cex=1,pch=y0.pch, pty="s",col = y0.color, xaxt="n")
for (i in 1:length(b0.all)) lines(rep(xid0[i],2),
                                  c(b0.all[i]-1.96*se0.all[i],b0.all[i]+1.96*se0.all[i]),
                                  col=y0.color[i], lty=y0.lty[i])
abline(h=0)
axis(1, at=xid0,labels=xid0, col.axis="black", las=2,cex.axis=0.7)

plot(xid1, b1.all, xlab = "Survey Year", xlim = c(min(d2$o_year),max(d2$o_year)), main="Party Identification (LPM)",
     ylim=c(-.6,.6), ylab="Coef. with 95% CI",cex=1,pch=y1.pch, pty="s",col = y1.color, xaxt="n")  
for (i in 1:length(b1.all)) lines(rep(xid1[i],2),
                                  c(b1.all[i]-1.96*se1.all[i],b1.all[i]+1.96*se1.all[i]),
                                  col=y1.color[i], lty=y1.lty[i])
abline(h=0)
axis(1, at=xid1,labels=xid1, col.axis="black", las=2,cex.axis=0.7)
dev.off()

